package com.css.fxfzdpzs.modules.hddc.repository.impl;

import com.css.fxfzdpzs.modules.hddc.entity.first.DifferentConut;
import com.css.fxfzdpzs.modules.hddc.entity.first.FaultNum;
import com.css.fxfzdpzs.modules.hddc.entity.first.FaultTotal;
import com.css.fxfzdpzs.modules.hddc.entity.second.FaultNumLen;
import com.css.fxfzdpzs.modules.hddc.repository.HddcRepository;
import com.css.fxfzdpzs.modules.hddc.vo.FaultNumVo;
import com.css.fxfzdpzs.modules.xczh.entity.first.DisasterDamage;
import com.css.fxfzdpzs.modules.xczh.entity.first.DisasterVisitsCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class HddcRepositoryImpl implements HddcRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public List<FaultNum> getFaultNum() {
        String sql = "SELECT * FROM hddc_fault_num";
        List<FaultNum> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FaultNum.class));
        return list;
    }

    @Override
    public FaultNumVo getFaultTotal() {
        String sql = "SELECT COALESCE(SUM(num), 0) AS faultTotal FROM hddc_fault_num";
        FaultNumVo faultTotal = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(FaultNumVo.class));
        return faultTotal;
    }

    @Override
    public List<DifferentConut> getDifferentConut() {
        String sql = "SELECT * FROM hddc_different_conut where province ='0'";
        List<DifferentConut> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DifferentConut.class));
        return list;
    }

    @Override
    public Integer getFaultNumByPro(String province) {
        String sql = "SELECT num FROM hddc_fault_num where province = '" + province + "'";

        Integer num = 0;
        try {
            num = jdbcTemplate.queryForObject(sql, Integer.class);
            return num;
        } catch (DataAccessException e) {
            return num;
        }
    }

    @Override
    public List<FaultNumLen> getFaultNumLenByPro(String province) {
        String sql = "SELECT * FROM hddc_fault_num_len where province = '" + province + "'";
        List<FaultNumLen> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FaultNumLen.class));
        return list;
    }

    @Override
    public List<DifferentConut> getDifferentConutByPro(String province) {
        String sql = "SELECT * FROM hddc_different_conut where province ='" + province + "'";
        List<DifferentConut> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DifferentConut.class));
        return list;

    }

    @Override
    public List<FaultNumVo> getGeom() {
        String sql = "SELECT province,num,code FROM hddc_fault_num";
        List<FaultNumVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FaultNumVo.class));
        return list;
    }

    @Override
    public void delFaultNum() {
        jdbcTemplate.update("delete from hddc_fault_num");
    }

    @Override
    public void saveFaultNum(List<FaultNum> faultNumList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(faultNumList.toArray());
        String sql = "INSERT INTO  hddc_fault_num  " +
                "(id,province,num,proportion,code) " +
                "values" +
                "(:id,:province,:num,:proportion,:code)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);

    }

    @Override
    public void delDifferentConut() {
        jdbcTemplate.update("delete from hddc_different_conut");
    }

    @Override
    public void saveDifferentConut(List<DifferentConut> differentConutList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(differentConutList.toArray());
        String sql = "INSERT INTO  hddc_different_conut  " +
                "(id,activity_era_type,activity_era_num,activity_era_proportion,province) " +
                "values" +
                "(:id,:activityEraType,:activityEraNum,:activityEraProportion,:province)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void delFaultNumLen() {
        jdbcTemplate.update("delete from hddc_fault_num_len");
    }

    @Override
    public void saveFaultNumLen(List<FaultNumLen> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO  hddc_fault_num_len  " +
                "(id,province,city,number,length,one_level,two_level,other) " +
                "values" +
                "(:id,:province,:city,:number,:length,:oneLevel,:twoLevel,:other)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }
}
